package com.example.designer.singleton;

/**
 * @author zhaoyunlong
 * @date 2020/12/16
 * @description
 *   静态内部类
 *   jvm  保证单例
 *   加载外部类时不会加载  内部类  这样就可以实现懒加载
 *   比singletonOne 好 因为对象是在真正调取的时候获取的。
 */
public class SingletonSix {

    public static SingletonSix instance;

    private SingletonSix() {

    }

    private static class SingletonHolder{

        private static final SingletonSix instance = new SingletonSix();
        public static SingletonSix getInstance(){
            return SingletonHolder.instance;
        }

    }


    public static void main(String[] args) {

        for (int i = 0; i < 100; i++) {
            new Thread(() -> {
                //不同对象的 hashcode是不一样的
                SingletonSix singleton2 = SingletonHolder.getInstance();
                System.out.println(singleton2.hashCode());
            }).start();
        }
    }
}
